package com.coollang.smartbra.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.coollang.smartbra.fragment.SportStatisticsFragment;
import com.coollang.smartbra.utils.ActionTypeParse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Timmy
 * @data: 2018/1/30
 * @Fuction:
 */

public class SportStatisticsPagerAdapter extends FragmentStatePagerAdapter {
    private List<Integer> mDataList;
    private Map<Integer, SportStatisticsFragment> mFragmentMap = new HashMap<>();

    public SportStatisticsPagerAdapter(FragmentManager fm, List<Integer> dataList) {
        super(fm);
        mDataList = dataList;
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public Fragment getItem(int position) {
        SportStatisticsFragment fragment = SportStatisticsFragment.newInstance(position);
        mFragmentMap.put(position, fragment);

        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return ActionTypeParse.getActionName(mDataList.get(position));
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        mFragmentMap.remove(position);
    }

    public SportStatisticsFragment getFragment(int key) {
        return mFragmentMap.get(key);
    }


}
